package com.intellagric.service.traceability.Impl.trace_record_table;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.dao.traceability.TableDao;
import com.intellagric.dao.traceability.TraceTableDao;
import com.intellagric.mapper.TraceTableMapper;
import com.intellagric.mapper.TraceTableRowsMapper;
import com.intellagric.module.traceability.CheckRecordVo;
import com.intellagric.pojo.*;
import com.intellagric.service.traceability.trace_record_table.TableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2019/2/22
 * @DESC： 表格类型操作服务
 */
@Service
public class TableServiceImpl implements TableService {

    @Autowired
    private TraceTableMapper traceTableMapper;

    @Autowired
    private TraceTableRowsMapper tableRowsMapper;

    @Autowired
    private TableDao tableDao;

    @Autowired
    private TraceTableDao traceTableDao;

    /**
     * 添加表格
     * @param table
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addTable(TraceTable table) {
        //创建表
        if (traceTableMapper.insertSelective(table) == 1) {
            //添加列
            List<String> rowList = new ArrayList<>();
            int i = 0;
            for (TraceTableRows tableRows : table.getTableRows()) {
                tableRows.setSize(i++);//排序字段
                tableRowsMapper.insertSelective(tableRows);
                rowList.add(tableRows.getName());
            }
            //创建实际表格
//            tableDao.createTable(table.getName(),rowList);
            tableDao.createTable(table.getName(),table.getTableRows());

            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除表格
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteTable(String id) {
        //删除表
        if (traceTableMapper.deleteByPrimaryKey(id) == 1) {
            //删除列
            TraceTableRowsExample tableRowsExample = new TraceTableRowsExample();
            tableRowsExample.createCriteria().andTableIdEqualTo(id);
            tableRowsMapper.deleteByExample(tableRowsExample);

            //删除实际表
            tableDao.deleteTable(id);//id和表名称相同
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改表格信息
     * @param table
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editTable(TraceTable table) {
        if (traceTableMapper.updateByPrimaryKeySelective(table) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 查询表格信息
     * @param id
     * @return TraceTable
     */
    @Override
    public TraceTable getTableById(String id) {
        return traceTableMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询表格信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getTablesList() {
        TraceTableExample tableExample = new TraceTableExample();
        tableExample.createCriteria().andIdIsNotNull();
        List<TraceTable> tableList = traceTableMapper.selectByExample(tableExample);
        return LayuiDataGridResult.success().add(tableList,tableList.size());
    }

    /**
     * 分页查询
     * @param page
     * @param rows
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getTablePage(int page, int rows) {
        PageHelper.startPage(page,rows);
        TraceTableExample tableExample = new TraceTableExample();
        tableExample.createCriteria().andIdIsNotNull();
        List<TraceTable> tableList = traceTableMapper.selectByExample(tableExample);
        PageInfo<TraceTable> pageInfo = new PageInfo<>(tableList);
        return LayuiDataGridResult.success().add(tableList,pageInfo.getTotal());
    }

    /**
     * 多条件分页查询
     * @param page
     * @param rows
     * @param table
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getTablePageByVo(int page, int rows, TraceTable table) {
        PageHelper.startPage(page,rows);

        List<TraceTable> tableList  =traceTableDao.selectTableByVo(table);
        PageInfo<TraceTable> pageInfo = new PageInfo<>(tableList);
        return LayuiDataGridResult.success().add(tableList,pageInfo.getTotal());
    }
}
